#!/bin/bash

g_Pro_SrcPath=$(pwd)/../..
g_WpaTool_Path=$(pwd)
source $g_Pro_SrcPath/ComEchoType.sh

g_ChipName="$1"
g_InstallName=""
g_Sh_Cross_Tool=""
g_Sh_Strip_Tool=""

Sh_Func_Usage()
{
    if [ "$g_ChipName" = "" ] ; then
        echo -e "\033[30;47m"$0" [chip]\033[0m"
        echo -e "\033[36m[chip]"
        ls $g_Pro_SrcPath/IPC_Code/ChipSdk --ignore=*.txt --ignore=*.h --ignore=*.c --ignore=GpioCtrl --ignore=PtzMotor --ignore=PwmCode --ignore=WatchDog
        echo -e "\033[0m"
        exit
    elif [ "$g_ChipName" = "clean" ] ; then
        rm $g_WpaTool_Path/openssl-1.1.1-pre7/_install/* -rf
        rm $g_WpaTool_Path/libnl-3.2.25/_install/* -rf
        sh_func_echo_info "clean build"
        exit
    fi
}

# 检测芯片平台的工具 #
Sh_Func_Check_ChipTools()
{
    if [ "$g_ChipName" = "AnyKaEV300L" ];then
        g_Sh_Cross_Tool=arm-anycloud-linux-uclibcgnueabi
        g_Sh_Strip_Tool=arm-anycloud-linux-uclibcgnueabi-strip
        g_InstallName="_install/AnyKaEV300L"
    elif [ "$g_ChipName" = "IngenicT41" ];then
        g_Sh_Cross_Tool=mips-linux-uclibc
        g_Sh_Strip_Tool=mips-linux-uclibc-strip
        g_InstallName="_install/IngenicT41"
    else
        echo -e "\033[31;47mNo such Chip $g_ChipName\033[0m"
        exit
    fi
}

Sh_Func_Openssl()
{
    openssl_path=$g_WpaTool_Path/openssl-1.1.1-pre7
    cd $g_WpaTool_Path/openssl-1.1.1-pre7

    if [ ! -d "_install" ];then
        sh_func_echo_info "create openssl _install"
        mkdir _install
    fi

    if [ -d "$g_InstallName" ];then
        sh_func_echo_warn "Openssl is builded"
        return
    fi

    mkdir $g_InstallName
    sh_func_echo_info "build openssl-1.1.1"
    ./Configure linux-armv4 enable-ec -fPIC no-async shared no-md2 no-mdc2 no-poly1305 no-blake2 no-siphash no-sm3 no-rc2 no-rc4 no-rc5 no-idea no-aria no-bf no-cast no-camellia no-seed no-sm4 no-chacha no-dsa no-sm2 no-dso no-engine no-err no-comp no-ocsp no-cms no-ts no-srp no-cmac no-ct --prefix=$openssl_path/$g_InstallName CROSS_COMPILE=$g_Sh_Cross_Tool"-"

    make -j8
    make install

    $g_Sh_Strip_Tool $openssl_path/$g_InstallName/lib/libcrypto.so.1.1

    chmod 777 libcrypto.so.1.1 libcrypto.so

    cp $openssl_path/$g_InstallName/lib/libcrypto.so.1.1 $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
    cp $openssl_path/$g_InstallName/lib/libcrypto.so $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
}

Sh_Func_Libnl()
{
    libnl_path=$g_WpaTool_Path/libnl-3.2.25
    cd $g_WpaTool_Path/libnl-3.2.25

    if [ ! -d "_install" ];then
        sh_func_echo_info "create libnl _install"
        mkdir _install
    fi

    if [ -d "$g_InstallName" ];then
        sh_func_echo_warn "libnl is builded"
        return
    fi
    mkdir $g_InstallName
    sh_func_echo_info "build libnl3.2.25"
    ./configure --host=$g_Sh_Cross_Tool CC=$g_Sh_Cross_Tool"-gcc" --prefix=$libnl_path/$g_InstallName

    make -j8
    make install

    $g_Sh_Strip_Tool $libnl_path/$g_InstallName/lib/libnl-3.so.200.20.0
    $g_Sh_Strip_Tool $libnl_path/$g_InstallName/lib/libnl-genl-3.so.200.20.0

    chmod 777 libnl-3.so.200.20.0 libnl-3.so.200 libnl-3.so libnl-3.so.200.20.0 libnl-genl-3.so.200 libnl-genl-3.so

    cp $libnl_path/$g_InstallName/lib/libnl-3.so.200.20.0 $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
    cp $libnl_path/$g_InstallName/lib/libnl-3.so.200 $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
    cp $libnl_path/$g_InstallName/lib/libnl-3.so $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/

    cp $ $libnl_path/$g_InstallName/lib/libnl-genl-3.so.200.20.0 $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
    cp $libnl_path/$g_InstallName/lib/libnl-genl-3.so.200 $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/
    cp $libnl_path/$g_InstallName/lib/libnl-genl-3.so $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/lib/

    make distclean
}

Sh_Func_WpaTools()
{
    cd $g_WpaTool_Path//hostapd_wpa_supplicant_2.10_ATBM_0.2/wpa_supplicant
    make clean
    cp $g_ChipName .config
    make -j8

    $g_Sh_Strip_Tool wpa_passphrase
    $g_Sh_Strip_Tool wpa_supplicant

    chmod 777 wpa_passphrase wpa_supplicant

    cp wpa_passphrase $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/usr/sbin/
    cp wpa_supplicant $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/usr/sbin/
}

Sh_Func_Hostapd()
{
    cd $g_WpaTool_Path//hostapd_wpa_supplicant_2.10_ATBM_0.2/hostapd
    make clean
    cp $g_ChipName .config
    make -j8

    $g_Sh_Strip_Tool hostapd

    chmod 777 hostapd

    cp hostapd $g_Pro_SrcPath/filesystem/$g_ChipName/filesys/usr/sbin/
}

Sh_Func_Usage
Sh_Func_Check_ChipTools
Sh_Func_Openssl
Sh_Func_Libnl
export PKG_CONFIG_PATH=$g_WpaTool_Path/libnl-3.2.25/$g_InstallName/lib/pkgconfig:$PKG_CONFIG_PATH
Sh_Func_WpaTools
Sh_Func_Hostapd
